﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ArkanoidX.Objects;

namespace ArkanoidX.UI
{
    public class HighScores : Window
    {
        Text Title;
        List<Text> Scores = new List<Text>();

        Rectangle gameRect;
        MainGame mainGame;

        public HighScores(MainGame game, Window parent)
            : base(game, parent)
        {
            Title = new Text(game);
            Title.Scale = new Vector2(2, 2);
            Title.Value = "High Scores";
            Title.Color = Color.Goldenrod;
            mainGame = (MainGame)game;
            
            gameRect = new Rectangle(0, 0, mainGame.Graphics.PreferredBackBufferWidth,
                mainGame.Graphics.PreferredBackBufferHeight);

            int width = gameRect.Width * 3 / 5;
            int height = gameRect.Height * 4 / 5;
            Vector2 size = new Vector2(width, height);
            this.Align(gameRect, AlignType.Center, size, 0, 0);

            Title.Align(this.boundingRect, Window.AlignType.TopCenter, 0, 20);

            this.AddChild(Title);

            float ftop = 20 + Title.Size.Y;
            for (int i = mainGame.Records.Count - 1; i >= 0; i--)
            {
                MainGame.PlayerRecord pr = mainGame.Records[i];
                Text text = new Text(mainGame);
                text.Scale = new Vector2(1.5f, 1.5f);
                String sName = pr.PlayerName;
                if (sName.Trim().Length == 0)
                {
                    sName = "<Anonymous>";
                }
                String Score = String.Format("{0}", pr.Score);
                int MaxLen = 16;
                if (sName.Length > MaxLen)
                {
                    sName = sName.Substring(1, MaxLen - 1) + "-";
                }
                int len = sName.Length + Score.Length;
                int dots = 24 - len;
                String s = new String('.', dots);

                text.Value = String.Format("{0} {1} {2}", sName, s, Score);
                text.Align(this.boundingRect, AlignType.TopCenter, 0, ftop);
                Scores.Add(text);

                ftop += text.Size.Y * 0.8f;
            }

            foreach (Text score in Scores)
            {
                this.AddChild(score);
            }
        }

        public override void Update(GameTime gameTime)
        {
            KeyboardState kbs = Keyboard.GetState();

            if (kbs.IsKeyDown(Keys.Enter) && !((MainGame)Game).lkbsUI.LastEnterPressed)
            {
                this.Close();
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
    }
}
